package com.qingcloud.fexecutor.base.utils;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.qingcloud.fexecutor.base.handler.ApiParamDTO;
import com.qingcloud.fexecutor.base.handler.ApiRequestParam;
import lombok.extern.slf4j.Slf4j;

import java.util.Collection;
import java.util.Objects;

/**
 * API参数工具
 */
@Slf4j
public class ApiParameterUtil {

    /**
     * API占位参数替换
     */
    public static ApiRequestParam replaceValue(ApiRequestParam requestParam, String json) {
        cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(json);
        //请求体参数
        String bodyJson = requestParam.getApiBodyJson();
        if (StrUtil.isNotBlank(bodyJson)) {
            if (bodyJson.contains("#{") || bodyJson.contains("${")) {
                //替换占位符变量
                Collection<String> params = CustomMapperUtils.getParameterName(bodyJson);
                for (String param : params) {
                    Object object = jsonObject.getByPath(param);
                    if (Objects.isNull(object)) {
                        bodyJson = bodyJson.replace("\"#{" + param + "}\"", "null")
                                .replace("\"${" + param + "}\"", "null");
                    } else {
                        if (object instanceof String) {
                            if (String.valueOf(object).startsWith("{")) {
                                object = JSONObject.parseObject(String.valueOf(object));
                            }
                            if (String.valueOf(object).startsWith("[")) {
                                object = com.alibaba.fastjson.JSONArray.parseObject(String.valueOf(object));
                            }
                        }
                        if (object instanceof String) {
                            bodyJson = bodyJson.replace("#{" + param + "}", String.valueOf(object))
                                    .replace("${" + param + "}", String.valueOf(object));
                        } else {
                            bodyJson = bodyJson.replace("\"#{" + param + "}\"", String.valueOf(object))
                                    .replace("\"${" + param + "}\"", String.valueOf(object));
                        }
                    }
                }
            }
            log.debug("执行API{}的参数json参数是{}", requestParam.getName(), bodyJson);
        }
        requestParam.setApiBodyJson(bodyJson);
        //其他参数
        for (ApiParamDTO dto : requestParam.getHeadParam()) {
            if (dto.getValue() != null) {
                if (dto.getValue().toString().contains("#{") || dto.getValue().toString().contains("${")) {
                    //redis命令中有参数变量
                    for (String realName : CustomMapperUtils.getParameterName(dto.getValue().toString())) {
                        dto.setValue(jsonObject.getByPath(realName));
                    }
                }
            }
            dto.setDefaultValue(dto.getValue());
        }
        for (ApiParamDTO dto : requestParam.getQueryParam()) {
            if (dto.getValue() != null) {
                if (dto.getValue().toString().contains("#{") || dto.getValue().toString().contains("${")) {
                    //redis命令中有参数变量
                    for (String realName : CustomMapperUtils.getParameterName(dto.getValue().toString())) {
                        dto.setValue(jsonObject.getByPath(realName));
                    }
                }
            }
            dto.setDefaultValue(dto.getValue());
        }
        for (ApiParamDTO dto : requestParam.getPathParam()) {
            if (dto.getValue() != null) {
                if (dto.getValue().toString().contains("#{") || dto.getValue().toString().contains("${")) {
                    //redis命令中有参数变量
                    for (String realName : CustomMapperUtils.getParameterName(dto.getValue().toString())) {
                        dto.setValue(jsonObject.getByPath(realName));
                    }
                }
            }
            dto.setDefaultValue(dto.getValue());
        }
        for (ApiParamDTO dto : requestParam.getFormParam()) {
            if (dto.getValue() != null) {
                if (dto.getValue().toString().contains("#{") || dto.getValue().toString().contains("${")) {
                    //redis命令中有参数变量
                    for (String realName : CustomMapperUtils.getParameterName(dto.getValue().toString())) {
                        dto.setValue(jsonObject.getByPath(realName));
                    }
                }
            }
            dto.setDefaultValue(dto.getValue());
        }
        return requestParam;
    }

    /**
     * API占位参数替换
     */
    public static String replaceValue(String bodyJson, String json) {
        cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(json);
        //请求体参数
        if (StrUtil.isNotBlank(bodyJson)) {
            if (bodyJson.contains("#{") || bodyJson.contains("${")) {
                //替换占位符变量
                Collection<String> params = CustomMapperUtils.getParameterName(bodyJson);
                for (String param : params) {
                    Object object = jsonObject.getByPath(param);
                    if (Objects.isNull(object)) {
                        bodyJson = bodyJson.replace("\"#{" + param + "}\"", "null")
                                .replace("\"${" + param + "}\"", "null");
                    } else {
                        if (object instanceof String) {
                            if (String.valueOf(object).startsWith("{")) {
                                object = JSONObject.parseObject(String.valueOf(object));
                            }
                            if (String.valueOf(object).startsWith("[")) {
                                object = com.alibaba.fastjson.JSONArray.parseObject(String.valueOf(object));
                            }
                        }
                        if (object instanceof String) {
                            bodyJson = bodyJson.replace("#{" + param + "}", String.valueOf(object))
                                    .replace("${" + param + "}", String.valueOf(object));
                        } else {
                            bodyJson = bodyJson.replace("\"#{" + param + "}\"", String.valueOf(object))
                                    .replace("\"${" + param + "}\"", String.valueOf(object));
                        }
                    }
                }
            }
            log.debug("替换参数结果 {}", bodyJson);
            return bodyJson;
        }
        return bodyJson;
    }

}
